package top.infopub.mgr.mkt.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.infopub.mgr.hr.dao.OrgInfoDao;
import top.infopub.mgr.hr.dao.StaffInfoDao;
import top.infopub.mgr.mkt.service.MarketManageService;
import top.infopub.model.hr.OrgInfo;
import top.infopub.model.hr.StaffInfo;
import top.infopub.model.mkt.ProjectInfo;
import top.infopub.security.vo.AuthorizationVO;

import java.util.List;

@Service
public class MarketManageServiceImpl implements MarketManageService {
    @Autowired
    private OrgInfoDao orgInfoDao;

    @Autowired
    private StaffInfoDao staffInfoDao;

    @Override
    public List<OrgInfo> queryOrgInfoByAuthUserId(AuthorizationVO authVo) {
        //所属组织id不为空查询对应的区域公司信息，系统管理员直接查询所有的
        String types = "1";
        String orgId = null;
        if (StringUtils.isNotBlank(authVo.getHrStaffId())) {
            //所属的组织id不为空
            OrgInfo orgInfo = orgInfoDao.queryOrgTypesByAuthUserId(authVo.getId());
            if (orgInfo == null) return null;
            orgId = orgInfo.getId();
            types = orgInfo.getTypes();
        }
        return orgInfoDao.queryOrgInfoByWhere(types, orgId);
    }

    @Override
    public List<StaffInfo> queryStaffInfoByAuthUserId(AuthorizationVO authVo) {
        //所属组织id不为空查询对应的区域公司信息，系统管理员直接查询所有的
        String types = "1";
        String orgId = null;
        if (StringUtils.isNotBlank(authVo.getHrStaffId())) {
            //所属的组织id不为空
            OrgInfo orgInfo = orgInfoDao.queryOrgTypesByAuthUserId(authVo.getId());
            if (orgInfo == null) return null;
            orgId = orgInfo.getId();
            types = orgInfo.getTypes();
        }
        //如果types为3(项目公司)，只查询对应的人员
        if (StringUtils.equals("3", types)) {
            orgId = authVo.getId();
        }
        /**
         * types说明
         * 1：查询TYPES为1和2的所有人员
         * 2：查询指定区域公司所有人员
         * 3：查询指定人员
         */
        return staffInfoDao.queryStaffInfoByWhere(types, orgId);
    }

    @Override
    public List<ProjectInfo> optionalProjectName() {
        return staffInfoDao.optionalProjectName();
    }
}
